/********************************************UI部分***********************************************/
"ui";
auto.waitFor();    //等待获取无障碍辅助权限
var form =
{
    isText: false,
}
let deviceWidth = device.width;
let deviceHeight = device.height;
let margin = parseInt(deviceWidth * 0.03);
let buttonWidth = parseInt(deviceWidth * 0.40);
ui.layout(
    <vertical margin={margin + "px"} gravity="left|top">
        <appbar>
            <toolbar id="toolbar" title="软件Title" />
        </appbar>
        <Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp" />
 
        <text textSize="16sp" textColor="red" text="使用前请确保无障碍服务和悬浮窗权限已经开启!" />
 
        <vertical>
            <button id="all" h="50" text="开始运行" />
        </vertical>
 
        <horizontal>
            <button margin={margin + "px"} id="dq" h="40" text="单独功能1" w={buttonWidth + "px"} />
            <button margin={margin + "px"} id="cq" h="40" text="单独功能2" w={buttonWidth + "px"} />
        </horizontal>
 
 
        <horizontal>
            <button margin={margin + "px"} id="wq" h="40" text="单独功能3" w={buttonWidth + "px"} />
            <button margin={margin + "px"} id="sq" h="40" text="单独功能4" w={buttonWidth + "px"} />
        </horizontal>
 
        <horizontal>
            <button margin={margin + "px"} id="update" h="40" text=" 单独功能5 " w={buttonWidth + "px"} />
            <button margin={margin + "px"} id="stop" h="40" text="停止运行" w={buttonWidth + "px"} />
        </horizontal>
 
        <horizontal>
            <text textSize="16sp" marginLeft="15" textColor="black" text="用户参数输入:" />
            <input id="acatlog" text="" />
        </horizontal>
 
        <vertical>
            <text text="用户参数选择" textColor="#222222" />
            <horizontal>
                <radio id="yes_read" text="是" checked="true"></radio>
                <radio id="no_read" text="否"></radio>
            </horizontal>
        </vertical>
 
 
        <text id={"resultLabel"} textColor="red" w="*" />
        <button w="250" layout_gravity="center" id="about" text="关于本软件" />
    </vertical>
);
 
//创建选项菜单(右上角)
ui.emitter.on("create_options_menu", menu => {
    menu.add("启动悬浮窗");
    menu.add("运行日志");
});
 
//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item) => {
    switch (item.getTitle()) {
        case "启动悬浮窗":
            var intent = new Intent();
            intent.setAction("android.settings.action.MANAGE_OVERLAY_PERMISSION");
            app.startActivity(intent);
            break;
        case "运行日志":
            app.startActivity('console');
            break;
    }
    e.consumed = true;
});
 
 
ui.yes_read.on("check", function (check) {
    if (check) {
        form.isText = true;
        textTarget = true;
    }
});
ui.no_read.on("check", function (check) {
    if (check) {
        form.isText = false;
        textTarget = false;
    }
});

function createFloaty(){
    threads.start(
        function(){
            closefloaty = floaty.rawWindow(
                <horizontal gravity="center" bg="#77ff0000">
                    <button id="closea" text="关闭脚本" w="auto" h="auto" />
                    <button id="move" text="移动" w="auto" h="auto" />       
                </horizontal>
            );
            var floatyX = 0.1*width
            var floatyY = 0.1*height
            var windowX,windowY;
            closefloaty.setPosition(floatyX,floatyY);
            ui.post(function(){
                windowWidth = closefloaty.getWidth();
                windowHeight = closefloaty.getHeight();
            },1000);
            closefloaty.move.setOnTouchListener(function(view,event){})
        }
    )
} 

createFloaty();
 
//ui.acatlog.setText(aCatlog.toString());
 
 
ui.autoService.on("check", function (checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({ action: "android.settings.ACCESSIBILITY_SETTINGS" });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});
 
// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function () {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});
 
 
 
var thread = null;
ui.all.click(function () {
    if (thread != null && thread.isAlive()) {
        alert("注意!", "当前程序正在运行，请结束之前进程");
        return;
    }
    toast("开始运行");
    thread = threads.start(function () {
        aCatlog = ui.acatlog.getText();
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            return;
        }
        main();   //主函数开始运行
    });
});
 
 
ui.cq.click(function () {
    if (thread != null && thread.isAlive()) {
        alert("注意!", "当前程序正在运行，请结束之前进程");
        return;
    }
    thread = threads.start(function () {
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            return;
        }
        //此处开始执行函数();
    });
});
 
 
ui.dq.click(function () {
    if (thread != null && thread.isAlive()) {
        alert("注意!", "当前程序正在运行，请结束之前进程");
        return;
    }
    thread = threads.start(function () {
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            return;
        }
        //此处开始执行函数();
    });
});
 
 
ui.wq.click(function () {
    if (thread != null && thread.isAlive()) {
        alert("注意!", "当前程序正在运行，请结束之前进程");
        return;
    }
    thread = threads.start(function () {
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            return;
        }
        //此处开始执行函数();
    });
});
 
ui.sq.click(function () {
    if (thread != null && thread.isAlive()) {
        alert("注意!", "当前程序正在运行，请结束之前进程");
        return;
    }
    thread = threads.start(function () {
        if (auto.service == null) {
            toastLog("请先开启无障碍服务！");
            return;
        }
        //此处开始执行函数();
    });
});
 
 
 
ui.stop.click(function () {
    if (thread != null && thread.isAlive()) {
        threads.shutDownAll();
        toast("已停止运行！")
        console.hide();
    }
    else {
        toast("当前没有线程在运行！")
    }
});
 
 
ui.about.click(function () {
    alert("说明", "本文中所有代码和举例均为学习交流之用，不得用于其他用途！");
});